<?php


namespace app\api\controller;


use app\api\model\AdminRole;
use app\common\controller\Api;
use app\common\controller\OperationLog;
use think\Db;
use think\Request;

/**
 * 管理员权限信息控制器
 * Class Auth
 * @package app\api\controller
 */
class Auth extends Api
{
    /**
     * 不需要登录权限的方法数组
     * @var string[]
     */
    protected $noNeedLogin = [];
    /**
     * 不需要鉴权的方法数组
     * @var array
     */
    protected $noNeedAuth = ['auth_list'];
    /**
     * 不需要验证token的方法数组
     * @var array
     */
    protected $noNeedToken = [];

    public function auth_list()
    {
        // 定义返回数据数组
        $data['menu'] = [];
        $data['permission'] = [];
        $data['router_data'] = [];
        // 定义日志参数
        $options = array(
            'title' => '获取权限列表',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        $param = Request::instance()->header();
        $validRes = $this->validate($param, 'Admin.auth');
        if ($validRes !== true) {
            OperationLog::OperError($param, $options, 'R', $validRes);
            $this->error('error', ['code' => 201, 'data' => $validRes]);
        }
        // 根据token获取用户的按钮权限，菜单权限
        $admin = \app\api\model\Admin::get(['token' => $param['token']]);
        if (empty($admin)) {
            OperationLog::OperError($param, $options, 'R', '管理员用户不存在!');
            $this->error('error', ['code' => 201, 'data' => '管理员用户不存在!']);
        } else {
            // 判断是否为超管，并获取对应的角色id
            if ($admin['root'] == 1) {
                $menuData = Db::name('system_menu')
                    ->where('type', 'in', ['C', 'M'])
                    ->where('is_disable', '=', 0)
                    ->where('is_show', '=', 1)
                    ->select();
                $permissionData = ['*'];
                $routerData = Db::name('system_menu')
                    ->order('sort', 'desc')
                    ->where('component', '<>', '')
                    ->select();
            } else {
                $role_id = AdminRole::get(['admin_id' => $admin['id']])->value('role_id');
                $auth_info = Db::name('system_role_menu')
                    ->where('role_id', '=', $role_id)
                    ->column('menu_id');
                $menuData = Db::name('system_menu')
                    ->where('type', 'in', ['C', 'M'])
                    ->where('is_disable', '=', 0)
                    ->where('is_show', '=', 1)
                    ->where('id', 'in', $auth_info)
                    ->select();
                $permissionData = Db::name('system_menu')
                    ->where('type', 'in', ['A'])
                    ->where('id', 'in', $auth_info)
                    ->column('perms');
                $routerData = Db::name('system_menu')
                    ->order('sort', 'desc')
                    ->where('component', '<>', '')
                    ->where('id', 'in', $auth_info)
                    ->select();
            }
            // 生成菜单和路由
            $data['menu_data'] = Api::build_menu_child(0, $menuData);
            $data['router_data'] = $routerData;
            $data['permission'] = $permissionData;
            OperationLog::OperSuccess($param, $options, 'R', $data);
            $this->success('success', ['code' => 200, 'data' => $data]);
        }
    }
}